/*
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-09-28 15:18:38
 * @LastEditTime: 2020-02-22 15:49:45
 * @LastEditors: Please set LastEditors
 */
Vue.component('form-detail', {
    data: function () {
        return {
            //是否显示简单下拉选
            showSimpleSelect: false,
            //简单下拉选的数组列表
            simpleColumns: [],
            //打开下拉选或者日期选择时，所指向的表单列
            pickerIndex: [0, 0],
            //是否显示时间选择器
            showDatePicker: false,
            //打开时间选择器时,选中的时间
            currentDate: new Date(),
            detailImportConfig: [],
            detailAnimateHeight: 0,

            columns: [],
            tableData: [],
            virtualTableData: [],
            cellData: {
                dom: '',
                data: '',
                row: ''
            },
            params: {},
            workList: [],
            crudList: [],
            currentClickCol: -1,
            paging: {
                pageSize: 30,
                pageIndex: 0,
                totalCount: 0
            },
            crud: {
                create: [],
                update: [],
                delete: []
            },
            selections: [],
            selectTableData: [],
            selectTableOriginData: [],
            selectColumns: [],
            selectTableColIndex: -1,
            //固定默认值，通过参数添加进来
            fixedValues: {},
            //是否显示合计行
            IsShowSummary: false,
            summaryData: [],
            //禁用业务流禁用数组
            unWorkRoleList: [],
            //表单控制权限
            billRoleSQL: "",
            changeDataSource: [],
            postDataSource: [],
            mainRowIndex: -1,
            stepperRowIndex: -1
        }
    },
    props: {
        objname: {
            type: String,
            default: ''
        },
        keyname: {
            type: String,
            default: ''
        },
        keyvalue: {
            type: Number,
            default: 0
        },
        modulename: {
            type: String,
            default: ''
        },
        draftkey: {
            type: Number,
            default: 0
        },
    },
    template: ' <div>\
                    <div class="p-select" v-if="mainRowIndex > -1">\
                        <van-row type="flex" justify="space-around">\
                            <van-col span="10" v-if="columns[mainRowIndex].isScan">\
                                <van-button type="primary" block size="small" @click="openScan">\
                                    <van-icon name="scan"></van-icon>\
                                    <span>扫描二维码</span>\
                                </van-button>\
                            </van-col>\
                            <van-col span="10" v-for="row in crudList" v-if="checkInsertShow(row)">\
                                <van-button type="primary" block size="small" @click="(row.name == \'新增\')?insertRow():\'\'">\
                                    <van-icon name="add-o"></van-icon>\
                                    <span>{{row.name}}</span>\
                                </van-button>\
                            </van-col>\
                            <van-col span="10" v-for="config in detailImportConfig" v-if="$parent.btn_showCondition(3,config)">\
                                <van-button type="primary" block size="small" @click="$parent.selfImportHandler(config)">\
                                    <van-icon name="orders-o"></van-icon>\
                                    <span>{{config.btnName}}</span>\
                                </van-button>\
                            </van-col>\
                        </van-row>\
                    </div>\
                    <div style="padding: 0 10px 15px;">\
                        <div class="shadow">\
                            <div class="content-label">子表明细</div>\
                            <div class="no-data" v-show="!tableData.length">暂无数据</div>\
                            <div>\
                                <div class="item" v-for="(row,index1) in tableData">\
                                    <div class="col-border">\
                                    <van-cell>\
                                        <div style="overflow:hidden;">\
                                            <div class="delete" @click="deleteRow(row,index1)"><img src="../../image/delete.png" /></div>\
                                            <div class="title">\
                                                <span v-if="columns[mainRowIndex].colType==2 && columns[mainRowIndex].selDetail.type==1">{{showSelectTableDisplayValue(row[\'colInfo\'][mainRowIndex].selDetail,row[columns[mainRowIndex].colName])}}</span>\
                                                <span v-else-if="columns[mainRowIndex].colType==2 && columns[mainRowIndex].selDetail.type==3">{{showSelectTableDisplayValueByAjax(row[\'colInfo\'][mainRowIndex].selDetail,row[columns[mainRowIndex].colName])}}</span>\
                                                <span v-else>{{row[columns[mainRowIndex].colName]}}</span>\
                                            </div>\
                                            <div class="main-right">\
                                            <van-stepper v-model="row[columns[stepperRowIndex].colName]" :disabled="columns[stepperRowIndex].isReadOnly || columns[stepperRowIndex].isVirtual" @change="changeStepper(index1,stepperRowIndex,columns[stepperRowIndex])" v-if="stepperRowIndex > -1 && !columns[stepperRowIndex].isReadOnly && !columns[stepperRowIndex].isVirtual"></van-stepper>\
                                            <span v-if="stepperRowIndex > -1 && (columns[stepperRowIndex].isReadOnly || columns[stepperRowIndex].isVirtual)">{{row[columns[stepperRowIndex].colName]}}</span>\
                                            <span class="arrow-toggle" @click="toggleShowDetail(row)"><img src="../../image/retract.png" :class="!row.showDetail?\'rotate\':\'\'" /></span>\
                                            </div>\
                                        </div>\
                                    </van-cell>\
                                    </div>\
                                    <div class="col-border" v-for="(col,index2) in columns" v-show="!col.isHidden && !col.isTitle && col.colType != 4 && !col.isFold">\
                                        <van-cell v-if="col.colDataType==\'datetime\'" :title="col.colDesc" :value="row[col.colName] || \'\'" @click="openDatePicker(index1,index2,row,col)">\
                                            <van-icon slot="right-icon" name="../../image/date.png" size="18" style="line-height: inherit;"></van-icon>\
                                        </van-cell>\
                                        <van-cell v-else-if="col.colDataType==\'bit\'" :title="col.colDesc">\
                                            <div style="text-align:right;"><van-switch v-model="row[col.colName]" size="22px" ></van-switch></div>\
                                        </van-cell>\
                                        <div v-else-if="col.colType==2">\
                                        <van-cell v-if="col.selDetail.type==1" :title="col.colDesc" :value="showSelectTableDisplayValue(row[\'colInfo\'][index2].selDetail,row[col.colName])" is-link arrow-direction="down" @click="openSimpleSelect(index1,index2,row,col)">\
                                        </van-cell>\
                                        <van-cell v-if="col.selDetail.type==3" :title="col.colDesc" :value="showSelectTableDisplayValueByAjax(row[\'colInfo\'][index2].selDetail,row[col.colName])" is-link @click="openComplexSelect(index1,index2,row,col)">\
                                        </van-cell>\
                                        </div>\
                                        <van-cell v-else-if="col.colType==4" :title="col.colDesc">\
                                            <div>\
                                                <van-stepper v-model="row[col.colName]" :disabled="col.isReadOnly || col.isVirtual" @change="changeStepper(index1,index2,col)" ></van-stepper>\
                                            </div>\
                                        </van-cell>\
                                        <van-field v-else :label="col.colDesc" v-model="row[col.colName]" :readonly="col.isReadOnly || col.isVirtual" @click="clickInput(index1,index2,row,col)" @change="updateRow(col,index2)">\
                                        </van-field>\
                                    </div>\
                                    <div class="detail" :class="!row.showDetail?\'hide\':\'\'" :style="{height:detailAnimateHeight + \'px\'}">\
                                        <div class="col-border" v-for="(col,index2) in columns" v-show="!col.isHidden && !col.isTitle && col.colType != 4 && col.isFold">\
                                            <van-cell v-if="col.colDataType==\'datetime\'" :title="col.colDesc" :value="row[col.colName] || \'\'" @click="openDatePicker(index1,index2,row,col)">\
                                                <van-icon slot="right-icon" name="../../image/date.png" size="18" style="line-height: inherit;"></van-icon>\
                                            </van-cell>\
                                            <van-cell v-else-if="col.colDataType==\'bit\'" :title="col.colDesc">\
                                                <div style="text-align:right;"><van-switch v-model="row[col.colName]" size="22px" ></van-switch></div>\
                                            </van-cell>\
                                            <div v-else-if="col.colType==2">\
                                            <van-cell v-if="col.selDetail.type==1" :title="col.colDesc" :value="showSelectTableDisplayValue(row[\'colInfo\'][index2].selDetail,row[col.colName])" is-link arrow-direction="down" @click="openSimpleSelect(index1,index2,row,col)">\
                                            </van-cell>\
                                            <van-cell v-if="col.selDetail.type==3" :title="col.colDesc" :value="showSelectTableDisplayValueByAjax(row[\'colInfo\'][index2].selDetail,row[col.colName])" is-link @click="openComplexSelect(index1,index2,row,col)">\
                                            </van-cell>\
                                            </div>\
                                            <van-cell v-else-if="col.colType==4" :title="col.colDesc">\
                                                <div>\
                                                    <van-stepper v-model="row[col.colName]" :disabled="col.isReadOnly || col.isVirtual" @change="changeStepper(index1,index2,col)" ></van-stepper>\
                                                </div>\
                                            </van-cell>\
                                            <van-field v-else :label="col.colDesc" v-model="row[col.colName]" :readonly="col.isReadOnly || col.isVirtual" @click="clickInput(index1,index2,row,col)" @change="updateRow(col,index2)">\
                                            </van-field>\
                                        </div>\
                                    </div>\
                                </div>\
                            </div>\
                        </div>\
                        <van-popup v-model="showSimpleSelect" position="bottom">\
                            <van-picker show-toolbar :columns="simpleColumns" v-if=" columns[pickerIndex[1]] && columns[pickerIndex[1]].selDetail" :value-key="columns[pickerIndex[1]].selDetail.displayValue" @cancel="showSimpleSelect = false" @confirm="simpleSelectChange"></van-picker>\
                        </van-popup>\
                        <van-popup v-model="showDatePicker" position="bottom">\
                            <van-datetime-picker v-model="currentDate" cancel-button-text="清空" type="date" @cancel="clearDatePicker" @confirm="confirmDatePicker" />\
                        </van-popup>\
                    </div>\
                </div>',

    watch: {
        tableData: {
            handler(val, oldVal) {
                this.$parent.getBottomTotal();
            },
            deep: true
        }
    },
    created: function () {
        var detailVm = this;
        //子表新增
        api.addEventListener({
            name: 'selectMainDetailArray'
        }, function (ret, err) {

            var array = ret.value.array;
            for (var m in array) {

                var newRecord = {};
                for (var i = 0; i < detailVm.columns.length; i++) {
                    newRecord[detailVm.columns[i].colName] = null;
                    //如果该参数有默认值
                    if (detailVm.fixedValues[detailVm.columns[i].colName] != undefined) {
                        newRecord[detailVm.columns[i].colName] = detailVm.fixedValues[detailVm.columns[i].colName];
                        //如果columns的列是基础下拉类型
                        if (detailVm.columns[i].colType == 2 && detailVm.columns[i].selDetail.type < 3) {
                            if (detailVm.columns[i].selDetail.sqlDataSource.length > 0) {
                                var dataType = typeof (detailVm.columns[i].selDetail.sqlDataSource[0][detailVm.columns[i].selDetail.hiddenValue]);
                                if (dataType != typeof (detailVm.fixedValues[detailVm.columns[i].colName])) {
                                    if (dataType == "int") {
                                        newRecord[detailVm.columns[i].colName] = parseInt(detailVm.fixedValues[detailVm.columns[i].colName]);
                                    }
                                    else if (dataType == "boolean") {
                                        newRecord[detailVm.columns[i].colName] = Boolean(parseInt(detailVm.fixedValues[detailVm.columns[i].colName]));
                                    }
                                }
                            }
                        }
                    }
                }
                if (detailVm.tableData.length == 0) {
                    newRecord.objNo = "1";
                }
                else {
                    newRecord.objNo = (parseInt(detailVm.tableData[detailVm.tableData.length - 1].objNo) + 1).toString();
                }

                newRecord[detailVm.columns[detailVm.mainRowIndex].colName] = array[m][detailVm.columns[detailVm.mainRowIndex].selDetail.hiddenValue];
                detailVm.tableData.push(newRecord);

                detailVm.refreshCol();
                detailVm.crud.create.push(newRecord);
                newRecord.colInfo = detailVm.columns;

                for (var j = 0; j < detailVm.columns.length; j++) {
                    if (detailVm.columns[j].colChange) {
                        detailVm.handlerControlChange(detailVm.tableData.length - 1, detailVm.columns[j].colChange);
                    }
                }

                if (detailVm.columns[detailVm.mainRowIndex].colCallBack) {
                    detailVm.handlerControlChange(detailVm.tableData.length - 1, detailVm.columns[detailVm.mainRowIndex].colCallBack);
                }

                if (detailVm.stepperRowIndex > -1) {
                    detailVm.changeStepperByAdd(detailVm.tableData.length - 1, detailVm.stepperRowIndex, detailVm.columns[detailVm.stepperRowIndex]);
                }


                detailVm.refreshCol();



            }
        })

        //复杂下拉选单选选中
        api.addEventListener({
            name: 'selectDetailRow'
        }, function (ret, err) {
            var row = ret.value.row;

            var col = detailVm.columns[detailVm.pickerIndex[1]];
            detailVm.tableData[detailVm.pickerIndex[0]][col.colName] = row[col.selDetail.hiddenValue];

            detailVm.updateRow(col, detailVm.pickerIndex[1]);

            if (col.colChange) {
                detailVm.handlerControlChange(detailVm.pickerIndex[1], col.colChange);
            }
            if (col.colCallBack) {
                detailVm.handlerControlChange(detailVm.pickerIndex[1], col.colCallBack);
            }
        })

        //复杂下拉选单选选中
        api.addEventListener({
            name: 'selectDetailArray'
        }, function (ret, err) {
            var array = ret.value.array;
            var col = detailVm.columns[detailVm.pickerIndex[1]];
            var idArray = [];
            for (var i in array) {
                idArray.push(array[i][col.selDetail.hiddenValue])
            }
            detailVm.tableData[detailVm.pickerIndex[0]][col.colName] = idArray.join();
            detailVm.updateRow(col, detailVm.pickerIndex[1]);

            if (col.colChange) {
                detailVm.handlerControlChange(detailVm.pickerIndex[1], col.colChange);
            }
            if (col.colCallBack) {
                detailVm.handlerControlChange(detailVm.pickerIndex[1], col.colCallBack);
            }
        })


        var tableConfig = {
            ajaxRequest: false,
            objName: '',
            searchCondition: '',
            //设置配置
            set: function (options) {
                detailVm.columns = options.columns;
                detailVm.params = options.params;
                detailVm.workList = options.workList;
                detailVm.crudList = options.crudList;
                var detailAnimateHeightIndex = 0;
                //设置表格默认值
                for (var i = 0; i < detailVm.columns.length; i++) {
                    //插入默认值
                    if (detailVm.columns[i].colDefaultValue) {
                        detailVm.fixedValues[detailVm.columns[i].colName] = detailVm.getSYDefaultValue(detailVm.columns[i].colDefaultValue);
                    }
                    //如果是三个小点查询的，查看查看列选项
                    if (detailVm.isSelectAreaPage) {
                        if (detailVm.columns[i].isSearchCol) {
                            detailVm.columns[i].isHidden = false;
                        }
                        else {
                            detailVm.columns[i].isHidden = true;
                        }
                    }
                    //是否显示合计行
                    if (detailVm.columns[i].colTotalFunction) {
                        detailVm.IsShowSummary = true;
                    }

                    if (!detailVm.columns[i].isHidden && !detailVm.columns[i].isTitle && detailVm.columns[i].colType != 4 && detailVm.columns[i].isFold) {
                        detailAnimateHeightIndex++;
                    }


                    if (!detailVm.columns[i].isHidden && detailVm.columns[i].isTitle) {
                        detailVm.mainRowIndex = parseInt(i);
                    }
                    if (!detailVm.columns[i].isHidden && detailVm.columns[i].colType == 4) {
                        detailVm.stepperRowIndex = parseInt(i);
                    }
                }
                detailVm.detailAnimateHeight = detailAnimateHeightIndex * 44;
                detailVm.paging.pageSize = 9999999;
                //数据格式
                $.post('http://106.15.33.39:9981/SQLData/GetDataByPhysicalTableName', {
                    programId: localStorage.getItem("programId"),
                    tableName: detailVm.params.physicalTableName,
                    isForm: detailVm.params.isFormList,
                    filterCondition: detailVm.params.filterCondition,
                    defaultOrder: detailVm.params.defaultOrder
                }, function (data) {

                    if (data.status == 200) {
                        if (data.data.ds.length > 0) {
                            detailVm.paging.totalCount = data.data.ds.length;
                        }
                        if (detailVm.draftkey) {
                            var sql = "select * from Draft where DraftID='" + detailVm.draftkey + "'";
                            var dbValue = DB.selectSqlSync(sql)[0];
                            detailVm.tableData = JSON.parse(dbValue.JSONStr2);
                            for (var i in detailVm.tableData) {
                                detailVm.crud.create.push(detailVm.tableData[i]);
                            }

                        } else {
                            detailVm.tableData = data.data.ds;
                        }

                        detailVm.refreshCol();
                        detailVm.handlerTableData();
                        detailVm.initGlobalTable(true);
                        detailVm.calSummaryData();
                    }
                    else {
                        //错误
                        detailVm.tableData = [];
                    }
                }, "json");
                //添加虚拟表格
                if (detailVm.isChildAreaPage) {
                    for (var k = 0; k < 20; k++) {
                        var newRecord = {};
                        for (var i = 0; i < detailVm.columns.length; i++) {
                            newRecord[detailVm.columns[i].colName] = null;
                        }
                        newRecord["objNo"] = "";
                        newRecord.colInfo = detailVm.columns;
                        detailVm.virtualTableData.push(newRecord);
                    }
                }
            },
            //初始化配置
            initConfig: function (objName, searchCondition) {
                tableConfig.objName = objName;
                tableConfig.searchCondition = searchCondition;
                if (objName == undefined) {
                    return;
                }
                $.ajax({
                    url: 'http://106.15.33.39:9981/APPFormList/GetDataByName',
                    dataType: "json",
                    type: "post",
                    data: {
                        programId: localStorage.getItem("programId"),
                        objName: objName
                    },
                    success: function (data) {
                        var filterCondition = data.data.ds[0]["filterCondition"];
                        if (filterCondition == null) {
                            filterCondition = "";
                        }
                        //加上表单控制权限
                        if (detailVm.billRoleSQL) {
                            if (filterCondition == '') {
                                filterCondition += detailVm.billRoleSQL;
                            }
                            else {
                                filterCondition += " and " + detailVm.billRoleSQL + " ";
                            }
                        }

                        searchCondition = ((searchCondition == undefined || searchCondition == '' || searchCondition == null) ? "1=1" : searchCondition);
                        if (filterCondition == '') {
                            filterCondition += searchCondition;
                        }
                        else {
                            filterCondition += " and " + searchCondition + " ";
                        }
                        var currentIsFormEdit = data.data.ds[0]["isFormEdit"];

                        tableConfig.set({
                            columns: JSON.parse(data.data.ds[0]["columnsConfig"]),
                            workList: JSON.parse(data.data.ds[0]["worksConfig"]),
                            crudList: JSON.parse(data.data.ds[0]["crudConfig"]),
                            params: {
                                isFormList: data.data.ds[0]["isFormList"],
                                physicalTableName: data.data.ds[0]["physicalTableName"],
                                isPaging: data.data.ds[0]["isPaging"],
                                filterCondition: filterCondition,
                                defaultOrder: data.data.ds[0]["defaultOrder"],
                                detailName: data.data.ds[0]["detailName"],
                                isFormEdit: currentIsFormEdit,
                                rowClassCol: data.data.ds[0]["rowClassCol"],
                                isTempImport: data.data.ds[0]["isTempImport"]
                            }
                        });
                    },
                    complete: function (XHR, TS) {
                        if (TS == "success") {
                            tableConfig.ajaxRequest = true;
                        }
                    }
                })
            }
        }
        tableConfig.initConfig(detailVm.modulename, (detailVm.keyname || 1) + "='" + (detailVm.keyvalue || 0) + "'");



    },

    methods: {
        checkInsertShow(row) {
            var detailVm = this;
            if (row.name != '编辑' && row.name != '删除') {
                if (row.show) {
                    return eval(row.show);
                } else {
                    return true;
                }
            } else {
                return false;
            }
        },
        // app逻辑
        //---------------------------
        //点击输入框
        clickInput(index1, index2, row, col) {
            var detailVm = this;
            detailVm.pickerIndex = [index1, index2];
        },
        //步进器改变
        changeStepper(index1, index2, col) {
            var detailVm = this;
            detailVm.pickerIndex = [index1, index2];
            detailVm.updateRow(col, detailVm.pickerIndex[1]);
        },
        //打开简单下拉选
        openSimpleSelect(index1, index2, row, col) {
            var detailVm = this;
            if (col.isReadOnly || col.isVirtual) {
                return false;
            }
            detailVm.pickerIndex = [index1, index2];

            detailVm.simpleColumns = row['colInfo'][index2].selDetail.sqlDataSource;
            detailVm.showSimpleSelect = true;
        },
        //简单下拉选选中
        simpleSelectChange(value, index) {
            var detailVm = this;
            var col = detailVm.columns[detailVm.pickerIndex[1]];
            detailVm.tableData[detailVm.pickerIndex[0]][col.colName] = detailVm.simpleColumns[index][col.selDetail.hiddenValue];
            detailVm.showSimpleSelect = false;

            detailVm.updateRow(col, detailVm.pickerIndex[1]);
        },
        //打开复杂下拉选
        openComplexSelect(index1, index2, row, col) {
            var detailVm = this;
            if (col.isReadOnly || col.isVirtual) {
                return false;
            }
            detailVm.pickerIndex = [index1, index2];
            var filter = col.selDetail.formSQL;
            for (var key in row) {
                var value = "'" + row[key] + "'";
                filter = eval("filter.replace(/" + "@" + key + "/g,value)")
            }
            api.openWin({
                name: '复杂筛选' + row['colInfo'][index2].selDetail.url,
                url: 'formConfigScreen.html',
                reload: true,
                slidBackEnabled: false,
                pageParam: {
                    filter: filter,
                    screenName: row['colInfo'][index2].selDetail.url,
                    allowClear: true,
                    type: row['colInfo'][index2].selDetail.chooseType,
                    isDetail: true,
                    objName: detailVm.objname
                }
            })
        },
        //打开日期选择器
        openDatePicker(index1, index2, row, col) {
            var detailVm = this;
            if (col.isReadOnly || col.isVirtual) {
                return false;
            }
            detailVm.pickerIndex = [index1, index2];
            detailVm.currentDate = row[col.colName];
            detailVm.showDatePicker = true;
        },
        //清空所选日期
        clearDatePicker() {
            var detailVm = this;

            var col = detailVm.columns[detailVm.pickerIndex[1]];
            detailVm.tableData[detailVm.pickerIndex[0]][col.colName] = '';
            detailVm.showDatePicker = false;
            detailVm.updateRow(col, detailVm.pickerIndex[1])
        },
        //确认当前所选日期
        confirmDatePicker(value) {
            var detailVm = this;
            var col = detailVm.columns[detailVm.pickerIndex[1]];
            detailVm.tableData[detailVm.pickerIndex[0]][col.colName] = FLD.getFormatDate(value).split(' ')[0];
            detailVm.showDatePicker = false;
        },

        changeStepperByAdd(index1, index2, col) {
            var detailVm = this;
            setTimeout(function () {
                detailVm.changeStepper(index1, index2, col);
            }, 200)

        },

        toggleShowDetail(row) {
            if (typeof row.showDetail === 'undefined') {
                this.$set(row, 'showDetail', true);
            } else {
                row.showDetail = !row.showDetail;
            }
        },

        openScan() {
            var detailVm = this;
            var FNScanner = api.require('FNScanner');
            FNScanner.open({
                autorotation: true
            }, function (ret, err) {
                if (ret) {
                    if (ret.eventType == 'success') {



                        var newRecord = {};
                        for (var i = 0; i < detailVm.columns.length; i++) {
                            newRecord[detailVm.columns[i].colName] = null;
                            //如果该参数有默认值
                            if (detailVm.fixedValues[detailVm.columns[i].colName] != undefined) {
                                newRecord[detailVm.columns[i].colName] = detailVm.fixedValues[detailVm.columns[i].colName];
                                //如果columns的列是基础下拉类型
                                if (detailVm.columns[i].colType == 2 && detailVm.columns[i].selDetail.type < 3) {
                                    if (detailVm.columns[i].selDetail.sqlDataSource.length > 0) {
                                        var dataType = typeof (detailVm.columns[i].selDetail.sqlDataSource[0][detailVm.columns[i].selDetail.hiddenValue]);
                                        if (dataType != typeof (detailVm.fixedValues[detailVm.columns[i].colName])) {
                                            if (dataType == "int") {
                                                newRecord[detailVm.columns[i].colName] = parseInt(detailVm.fixedValues[detailVm.columns[i].colName]);
                                            }
                                            else if (dataType == "boolean") {
                                                newRecord[detailVm.columns[i].colName] = Boolean(parseInt(detailVm.fixedValues[detailVm.columns[i].colName]));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (detailVm.tableData.length == 0) {
                            newRecord.objNo = "1";
                        }
                        else {
                            newRecord.objNo = (parseInt(detailVm.tableData[detailVm.tableData.length - 1].objNo) + 1).toString();
                        }

                        detailVm.tableData.push(newRecord);

                        detailVm.refreshCol();
                        detailVm.crud.create.push(newRecord);
                        newRecord.colInfo = detailVm.columns;

                        for (var j = 0; j < detailVm.columns.length; j++) {
                            if (detailVm.columns[j].colChange) {
                                detailVm.handlerControlChange(detailVm.tableData.length - 1, detailVm.columns[j].colChange);
                            }
                        }



                        var searchKey = ret.content;
                        var searchParam = [];
                        for (var key in newRecord) {
                            searchParam.push({
                                Key: key,
                                Value: newRecord[key]
                            })
                        }
                        if (detailVm.columns[detailVm.mainRowIndex].selDetail.filterSQL) {
                            var sql = detailVm.columns[detailVm.mainRowIndex].selDetail.dataSource + " where " + detailVm.columns[detailVm.mainRowIndex].selDetail.filterSQL;
                            if (detailVm.columns[detailVm.mainRowIndex].selDetail.formSQL) {
                                sql += " and " + detailVm.columns[detailVm.mainRowIndex].selDetail.formSQL;
                            }
                            // alert(JSON.stringify({
                            //     programId: localStorage.getItem("programId"),
                            //     sql: sql,
                            //     moduleUrl: detailVm.columns[detailVm.mainRowIndex].selDetail.url,
                            //     searchKey: searchKey,
                            //     sqlParam: searchParam
                            // }))
                            $.post("http://106.15.33.39:9981/SQLData/QueryQRcodeFilterSQL", {
                                programId: localStorage.getItem("programId"),
                                sql: sql,
                                moduleUrl: detailVm.columns[detailVm.mainRowIndex].selDetail.url,
                                searchKey: searchKey,
                                sqlParam: searchParam
                            }, function (data) {
                                //单条数据
                                // alert(JSON.stringify(data))
                                if (data.status == 200) {
                                    var selectKeyValue = data.data[0][detailVm.columns[detailVm.mainRowIndex].selDetail.hiddenValue];

                                    if (selectKeyValue == "") {
                                        newRecord[detailVm.columns[detailVm.mainRowIndex].colName] = null;
                                    }
                                    else {
                                        newRecord[detailVm.columns[detailVm.mainRowIndex].colName] = selectKeyValue;
                                    }
                                    detailVm.refreshCol();
                                    //如果有change事件
                                    if (detailVm.columns[detailVm.mainRowIndex].colChange) {
                                        detailVm.handlerControlChange(detailVm.tableData.length - 1, detailVm.columns[detailVm.mainRowIndex].colChange);
                                    }
                                    if (detailVm.columns[detailVm.mainRowIndex].colCallBack) {
                                        detailVm.handlerControlChange(detailVm.tableData.length - 1, detailVm.columns[detailVm.mainRowIndex].colCallBack);
                                    }
                                    detailVm.refreshCol();
                                } else {
                                    layer.open({
                                        content: data.msg,
                                        skin: 'msg',
                                        time: 3
                                    });
                                    detailVm.tableData.splice(detailVm.tableData.length - 1, 1);
                                    detailVm.crud.create.splice(detailVm.crud.create.length - 1, 1);
                                }
                            }, "json");
                        }




                    }
                } else {
                    layer.open({
                        content: '扫描失败!',
                        skin: 'msg',
                        time: 3
                    });

                }
            });
        },

        //分页事件
        handleSizeChange(pageIndex) {
            var detailVm = this;
            //清空增删改
            detailVm.crud.create = [];
            detailVm.crud.update = [];
            detailVm.crud.delete = [];
            detailVm.paging.pageIndex = pageIndex;
            var queryColumns = [];
            for (var i = 0; i < detailVm.columns.length; i++) {
                //插入queryWhere
                var queryKey = "";
                var queryWhere = "";

                queryColumns.push({
                    queryKey: queryKey,
                    colName: detailVm.columns[i].colName,
                    queryWhere: queryWhere,
                    filter_selection: detailVm.columns[i].filter_selection,
                    querySort: detailVm.columns[i].querySort
                })
            }

            //查询
            $.post('http://106.15.33.39:9981/SQLData/GetDataByPhysicalTableNameByQueryPaging', {
                programId: localStorage.getItem("programId"),
                tableName: detailVm.params.physicalTableName,
                columns: queryColumns,
                currentCol: detailVm.currentColName,
                pageIndex: detailVm.paging.pageIndex,
                pageSize: detailVm.paging.pageSize,
                isForm: detailVm.params.isFormList,
                filterCondition: detailVm.params.filterCondition,
                defaultOrder: detailVm.params.defaultOrder
            }, function (data) {
                if (data.status == 200) {
                    detailVm.tableData = data.data.ds;
                    if (data.data.ds1.length > 0) {
                        //绑定总长度
                        detailVm.paging.totalCount = data.data.ds1[0]["@TotalCount"];
                    }
                    detailVm.refreshDataCol();
                    detailVm.handlerTableData();
                    detailVm.initGlobalTable(true);
                }
                else {
                    detailVm.tableData = [];
                }
            }, "json");
        },
        //插入导入数据
        insertImportData(record) {
            var detailVm = this;
            detailVm.tableData = detailVm.tableData.filter(function (item) { return !item.isVirtual });
            for (var i = 0; i < record.length; i++) {
                detailVm.insertRowByImport();
                //把不同的记录的列change执行
                for (var key in record[i]) {
                    var columnDetail = detailVm.columns.filter(function (item) { return item.colName == key });
                    if (columnDetail.length > 0) {
                        detailVm.tableData[detailVm.tableData.length - 1][key] = record[i][key];

                    }
                }
            }
            // alert(JSON.stringify(detailVm.tableData))
            detailVm.refreshCol();
            //
            detailVm.initGlobalTable();
        },
        insertRowByImport() {
            var detailVm = this;
            var newRecord = {};
            for (var i = 0; i < detailVm.columns.length; i++) {
                newRecord[detailVm.columns[i].colName] = null;
                //如果该参数有默认值
                if (detailVm.fixedValues[detailVm.columns[i].colName] != undefined) {
                    newRecord[detailVm.columns[i].colName] = detailVm.fixedValues[detailVm.columns[i].colName];
                    //如果columns的列是基础下拉类型
                    if (detailVm.columns[i].colType == 2 && detailVm.columns[i].selDetail.type < 3) {
                        if (detailVm.columns[i].selDetail.sqlDataSource.length > 0) {
                            var dataType = typeof (detailVm.columns[i].selDetail.sqlDataSource[0][detailVm.columns[i].selDetail.hiddenValue]);
                            if (dataType != typeof (detailVm.fixedValues[detailVm.columns[i].colName])) {
                                if (dataType == "int") {
                                    newRecord[detailVm.columns[i].colName] = parseInt(detailVm.fixedValues[detailVm.columns[i].colName]);
                                }
                                else if (dataType == "boolean") {
                                    newRecord[detailVm.columns[i].colName] = Boolean(parseInt(detailVm.fixedValues[detailVm.columns[i].colName]));
                                }
                            }
                        }
                    }
                }
            }
            if (detailVm.tableData.length == 0) {
                newRecord.objNo = "1";
            }
            else {
                newRecord.objNo = (parseInt(detailVm.tableData[detailVm.tableData.length - 1].objNo) + 1).toString();
            }
            detailVm.tableData.push(newRecord);
            if (detailVm.tableData.length == 1) {
                detailVm.refreshCol();
            }

            detailVm.crud.create.push(newRecord);
            newRecord.colInfo = detailVm.columns;
            for (var j = 0; j < detailVm.columns.length; j++) {
                if (detailVm.columns[j].colChange) {
                    detailVm.handlerControlChange(detailVm.tableData.length - 1, detailVm.columns[j].colChange);
                }
            }
        },
        //处理全表数据表
        handlerTableData() {
            var detailVm = this;
            var virtualCols = detailVm.columns.filter(function (item) { return item.isVirtual == true });
            for (var i = 0; i < virtualCols.length; i++) {
                for (var j = 0; j < detailVm.tableData.length; j++) {
                    var virtualEvent = virtualCols[i].virtualConfig.replace(/\['/g, " detailVm.tableData[j]['").replace(/vm\./g, "detailVm\.");
                    virtualEvent = virtualEvent.replace(/\["/g, " detailVm.tableData[j][\"").replace(/vm\./g, "detailVm\.");

                    detailVm.tableData[j][virtualCols[i].colName] = eval(virtualEvent);
                }
            }
        },
        //处理单条数据表
        handlerTableDataByRowIndex(rowIndex) {
            var detailVm = this;
            var virtualCols = detailVm.columns.filter(function (item) { return item.isVirtual == true });
            for (var i = 0; i < virtualCols.length; i++) {
                var currentObj = detailVm.tableData[rowIndex];
                var virtualEvent = virtualCols[i].virtualConfig.replace(/\['/g, " detailVm.tableData[rowIndex]['").replace(/vm\./g, "detailVm\.");
                virtualEvent = virtualEvent.replace(/\["/g, " detailVm.tableData[rowIndex][\"").replace(/vm\./g, "detailVm\.");

                currentObj[virtualCols[i].colName] = eval(virtualEvent);
                detailVm.$set(detailVm.tableData, rowIndex, currentObj);
                //detailVm.tableData[rowIndex][virtualCols[i].colName] = eval(virtualCols[i].virtualConfig.replace(/\[/g, " detailVm.tableData[rowIndex]["));
            }
        },
        //处理单条控件事件
        handlerControlChange(rowIndex, changeEvent) {
            var detailVm = this;
            // alert(rowIndex)
            try {
                var currentObj = undefined;
                if (typeof rowIndex === 'number') {
                    currentObj = detailVm.tableData[rowIndex];
                }
                else {
                    currentObj = rowIndex;
                }
                //eval(changeEvent.replace(/\[/g, " currentObj["));

                changeEvent = changeEvent.replace(/\['/g, " currentObj['").replace(/vm\./g, "detailVm\.");
                changeEvent = changeEvent.replace(/\["/g, " currentObj[\"").replace(/vm\./g, "detailVm\.");
                eval(changeEvent);

                detailVm.$set(detailVm.tableData, rowIndex, currentObj);
            }
            catch (e) {
                // layer.alert("出错事件：" + changeEvent + "\r\t出错日志：" + e.toString());
            }


        },
        //全局表格项
        initGlobalTable(disChange, isCallBack) {
            var detailVm = this;
            //添加objNo
            for (var i = 0; i < detailVm.tableData.length; i++) {
                detailVm.tableData[i].objNo = (i + 1).toString();
                detailVm.tableData[i].colInfo = detailVm.columns;
                if (!disChange) {
                    for (var j = 0; j < detailVm.columns.length; j++) {
                        if (detailVm.columns[j].colChange) {
                            detailVm.handlerControlChange(i, detailVm.columns[j].colChange);
                        }
                    }
                }
                if (isCallBack) {
                    for (var j = 0; j < detailVm.columns.length; j++) {
                        if (detailVm.columns[j].colCallBack) {
                            detailVm.handlerControlChange(i, detailVm.columns[j].colCallBack);
                        }
                    }
                }
            }
        },
        //新增行
        insertRow() {
            var detailVm = this;
            var filter = detailVm.columns[detailVm.mainRowIndex].selDetail.formSQL;
            for (var key in detailVm.tableData) {
                var value = "'" + detailVm.tableData[key] + "'";
                filter = eval("filter.replace(/" + "@" + key + "/g,value)")
            }
            api.openWin({
                name: '子表新增',
                url: 'formConfigScreen.html',
                reload: true,
                slidBackEnabled: false,
                pageParam: {
                    filter: filter,
                    screenName: detailVm.columns[detailVm.mainRowIndex].selDetail.url,
                    allowClear: true,
                    type: detailVm.columns[detailVm.mainRowIndex].selDetail.chooseType,
                    isDetail: false,
                    isButton: true
                }
            })
            return;
        },
        //删除行
        deleteRow(row) {
            var detailVm = this;
            detailVm.$dialog.confirm({
                title: '提示',
                message: '是否删除该条记录?'
            }).then(() => {
                if (row.objNo) {
                    var copyCreate = detailVm.crud.create.filter(function (item) { return item.objNo == row.objNo; });
                    detailVm.crud.create = detailVm.crud.create.filter(function (item) { return item.objNo != row.objNo; });
                    detailVm.tableData = detailVm.tableData.filter(function (item) { return item.objNo != row.objNo; });
                    //去除新增的行操作
                    if (copyCreate.length == 0) {
                        detailVm.crud.delete.push(row);
                    }

                    var copyUpdate = detailVm.crud.update.filter(function (item) { return item.objNo == row.objNo; });
                    detailVm.crud.update = detailVm.crud.update.filter(function (item) { return item.objNo != row.objNo; });
                }


            }).catch(() => {

            });



        },
        //更新行
        updateRow(col, index, sampleUpdate) {
            var detailVm = this;

            if (detailVm.crud.create.filter(function (item) { return item.objNo == detailVm.tableData[detailVm.pickerIndex[0]].objNo; }).length == 0) {
                if (detailVm.crud.update.filter(function (item) { return item.objNo == detailVm.tableData[detailVm.pickerIndex[0]].objNo; }).length == 0) {
                    detailVm.crud.update.push(detailVm.tableData[detailVm.pickerIndex[0]]);
                }
                else {
                    detailVm.crud.update = detailVm.crud.update.filter(function (item) { return item.objNo != detailVm.tableData[detailVm.pickerIndex[0]].objNo; })
                    detailVm.crud.update.push(detailVm.tableData[detailVm.pickerIndex[0]]);
                }
            }
            else {
                detailVm.crud.create = detailVm.crud.create.filter(function (item) { return item.objNo != detailVm.tableData[detailVm.pickerIndex[0]].objNo; });
                detailVm.crud.create.push(detailVm.tableData[detailVm.pickerIndex[0]]);
            }
            detailVm.handlerTableDataByRowIndex(detailVm.pickerIndex[0]);
            if (!sampleUpdate) {
                if (col.colChange) {
                    detailVm.handlerControlChange(detailVm.pickerIndex[0], col.colChange);
                }
                if (col.colCallBack) {
                    detailVm.handlerControlChange(detailVm.pickerIndex[0], col.colCallBack);
                }
            }
        },



        //子表保存行操作
        saveRow() {
            var detailVm = this;
            var execResult = true;
            var createObjs = [], updateObjs = [];
            for (var i = 0; i < detailVm.tableData.length; i++) {
                var createArray = detailVm.crud.create.filter(function (item) { return item.objNo == detailVm.tableData[i].objNo; });
                if (createArray.length > 0) {
                    createObjs.push(createArray[0]);
                }
                var updateArray = detailVm.crud.update.filter(function (item) { return item.objNo == detailVm.tableData[i].objNo; });
                if (updateArray.length > 0) {
                    updateObjs.push(updateArray[0]);
                }
            }
            //验证必填项
            var result = detailVm.valitedForm(createObjs, updateObjs);
            if (result.valited) {
                $.ajax({
                    type: 'post',
                    data: {
                        programId: localStorage.getItem("programId"),
                        tableName: detailVm.params.physicalTableName,
                        create: createObjs,
                        update: updateObjs,
                        delete: detailVm.crud.delete,
                        createWork: detailVm.crudList[0].sql,
                        updateWork: detailVm.crudList[1].sql,
                        deleteWork: detailVm.crudList[2].sql
                    },
                    url: "http://106.15.33.39:9981/FormList/SaveData",
                    async: false,
                    dataType: 'json',
                    success: function (data) {
                        if (data.status == 500) {
                            layer.open({
                                content: data.msg,
                                skin: 'msg',
                                time: 3
                            });
                            execResult = false;
                        }
                        else {
                            execResult = true;
                            // layer.open({
                            //     content: "保存成功",
                            //     skin: 'msg',
                            //     time: 3
                            // });
                            detailVm.handleSizeChange(detailVm.paging.pageIndex);

                        }
                    }
                });

            }
            else {
                layer.open({
                    content: result.valitedColumn + "不能为空!",
                    skin: 'msg',
                    time: 3
                });
                execResult = false;
            }

            return execResult;

        },

        //验证表单
        valitedForm(createObjs, updateObjs) {
            var detailVm = this;
            var valited = true;
            var valitedColumn = "";
            var index = 0;
            for (var i = 0; i < detailVm.columns.length; i++) {
                if (detailVm.columns[i].isRequired) {
                    for (var j = 0; j < createObjs.length; j++) {
                        if (createObjs[j][detailVm.columns[i].colName] === undefined || createObjs[j][detailVm.columns[i].colName] === null || createObjs[j][detailVm.columns[i].colName] === "") {
                            index = j;
                            valited = false;
                            valitedColumn = detailVm.columns[i].colDesc;
                        }
                    }
                    for (var j = 0; j < updateObjs.length; j++) {
                        if (updateObjs[j][detailVm.columns[i].colName] === undefined || updateObjs[j][detailVm.columns[i].colName] === null || updateObjs[j][detailVm.columns[i].colName] === "") {
                            index = j;
                            valited = false;
                            valitedColumn = detailVm.columns[i].colDesc;
                        }
                    }
                }
            }
            return {
                valited: valited,
                valitedColumn: valitedColumn,
                index: index
            };
        },
        //刷新列数据
        refreshCol() {
            var detailVm = this;
            for (var i = 0; i < detailVm.columns.length; i++) {
                detailVm.$set(detailVm.columns[i], "filter_selection", []);
                detailVm.$set(detailVm.columns[i], 'queryWhere', "like");
                detailVm.$set(detailVm.columns[i], "querySort", "asc");
                detailVm.$set(detailVm.columns[i], "queryKey", "");
                detailVm.$set(detailVm.columns[i], "visible", false);
                //下拉框数据加载
                if (detailVm.columns[i].colType == 2 && detailVm.columns[i].selDetail.type < 3) {
                    var sql = detailVm.columns[i].selDetail.dataSource;
                    data = detailVm.changeDataAjax(sql, "1=1");
                    detailVm.$set(detailVm.columns[i].selDetail, "sqlDataSource", data);
                    detailVm.$set(detailVm.columns[i].selDetail, "useRowNo", "0");
                    detailVm.$set(detailVm.columns[i], 'filters', data);
                    detailVm.$set(detailVm.columns[i], 'filterKeyWord', "");
                }
                else if (detailVm.columns[i].colType == 2 && detailVm.columns[i].selDetail.type == 3) {
                    var valueList = [];
                    for (var j = 0; j < detailVm.tableData.length; j++) {
                        if (detailVm.tableData[j][detailVm.columns[i].colName] != null && detailVm.tableData[j][detailVm.columns[i].colName] != undefined && detailVm.tableData[j][detailVm.columns[i].colName] != '') {
                            for (var k = 0; k < detailVm.tableData[j][detailVm.columns[i].colName].toString().split(',').length; k++) {
                                valueList.push("'" + detailVm.tableData[j][detailVm.columns[i].colName].toString().split(',')[k] + "'");
                            }
                        }
                    }
                    if (valueList.length > 0) {
                        var data = detailVm.changeDataAjax(detailVm.columns[i].selDetail.dataSource, detailVm.columns[i].selDetail.hiddenValue + " in (" + valueList.join(',') + ") ");
                        detailVm.$set(detailVm.columns[i].selDetail, "sqlDataSource", data);
                        detailVm.$set(detailVm.columns[i], 'filters', []);
                        detailVm.$set(detailVm.columns[i], 'filterKeyWord', "");
                    }
                    else {
                        detailVm.$set(detailVm.columns[i].selDetail, "sqlDataSource", []);
                        detailVm.$set(detailVm.columns[i], 'filters', []);
                        detailVm.$set(detailVm.columns[i], 'filterKeyWord', "");
                    }


                }
            }
        },

        //只刷新列，不刷新选择和选中项
        refreshDataCol() {
            var detailVm = this;
            for (var i = 0; i < detailVm.columns.length; i++) {
                //下拉框数据加载
                if (detailVm.columns[i].colType == 2 && detailVm.columns[i].selDetail.type < 3) {
                    var sql = detailVm.columns[i].selDetail.dataSource;
                    $.ajax({
                        url: "http://106.15.33.39:9981/SQLData/GetDataBySql",
                        async: false,
                        data: {
                            programId: localStorage.getItem("programId"),
                            sql: sql
                        },
                        type: "post",
                        dataType: 'json',
                        success: function (data) {
                            if (data.status == 200) {
                                detailVm.$set(detailVm.columns[i].selDetail, "sqlDataSource", data.data.ds);
                                detailVm.$set(detailVm.columns[i], 'filters', data.data.ds);
                            }
                        }
                    });
                }
                else if (detailVm.columns[i].colType == 2 && detailVm.columns[i].selDetail.type == 3) {
                    var valueList = [];
                    for (var j = 0; j < detailVm.tableData.length; j++) {
                        //if (detailVm.tableData[j][detailVm.columns[i].selDetail.hiddenValue] != null && detailVm.tableData[j][detailVm.columns[i].selDetail.hiddenValue] != undefined && detailVm.tableData[j][detailVm.columns[i].selDetail.hiddenValue] != '') {
                        //    valueList.push("'" + detailVm.tableData[j][detailVm.columns[i].selDetail.hiddenValue] + "'");
                        //}
                        if (detailVm.tableData[j][detailVm.columns[i].colName] != null && detailVm.tableData[j][detailVm.columns[i].colName] != undefined && detailVm.tableData[j][detailVm.columns[i].colName] != '') {
                            for (var k = 0; k < detailVm.tableData[j][detailVm.columns[i].colName].toString().split(',').length; k++) {
                                valueList.push("'" + detailVm.tableData[j][detailVm.columns[i].colName].toString().split(',')[k] + "'");
                            }
                        }


                    }
                    var sql = "select * from (" + detailVm.columns[i].selDetail.dataSource + ")t";
                    if (valueList.length > 0) {
                        sql += " where " + detailVm.columns[i].selDetail.hiddenValue + " in (" + valueList.join(',') + ") ";
                        $.ajax({
                            url: "http://106.15.33.39:9981/SQLData/GetDataBySql",
                            async: false,
                            data: {
                                programId: localStorage.getItem("programId"),
                                sql: sql
                            },
                            type: "post",
                            dataType: 'json',
                            success: function (data) {
                                if (data.status == 200) {
                                    detailVm.$set(detailVm.columns[i].selDetail, "sqlDataSource", data.data.ds);
                                    detailVm.$set(detailVm.columns[i], 'filters', []);
                                }
                            }
                        });
                    }
                    else {
                        detailVm.$set(detailVm.columns[i].selDetail, "sqlDataSource", []);
                        detailVm.$set(detailVm.columns[i], 'filters', []);
                    }
                }
            }
        },


        showSelectTableDisplayValue(db, selectValue) {
            var dbList = db.sqlDataSource.filter(function (item) { return item[db.hiddenValue] == selectValue })
            if (dbList.length > 0) {
                return dbList[0][db.displayValue];
            }
            else {
                return "";
            }
        },
        //通过ajax获取显示值，增加性能查询
        showSelectTableDisplayValueByAjax(db, selectValue) {
            var detailVm = this;
            if (db.chooseType == 1 || db.chooseType == 2) {
                try {
                    var dbList = db.sqlDataSource.filter(function (item) { return item[db.hiddenValue] == selectValue })
                    if (dbList.length > 0) {
                        return dbList[0][db.displayValue];
                    }
                    else {
                        return "";
                    }
                } catch (e) {
                    return "";
                }
            }
            else if (db.chooseType == 3) {
                try {
                    var dbList = db.sqlDataSource.filter(function (item) { return ("," + selectValue + ",").indexOf("," + item[db.hiddenValue] + ",") >= 0 })
                    if (dbList.length > 0) {
                        var resultArray = [];
                        for (var i = 0; i < dbList.length; i++) {
                            resultArray.push(dbList[i][db.displayValue]);
                        }
                        return resultArray.join(',');
                    }
                    else {
                        return "";
                    }
                } catch (e) {
                    return "";
                }
            }
            return "";
        },
        //三个点过滤数据
        filterKeypress($event, col) {
            var detailVm = this;
            var searchKey = $($event.currentTarget).find("input").val();
            if (searchKey == "") {
                col.filters = [];
            }
            else {
                var sql = col.selDetail.dataSource + " where " + col.selDetail.displayValue + " like '%" + searchKey + "%' ";
                $.post("http://106.15.33.39:9981/SQLData/GetDataBySql", {
                    programId: localStorage.getItem("programId"),
                    sql: sql
                }, function (data) {
                    col.filters = data.data.ds;
                }, "json");
            }

        },

        /*基础下拉事件*/
        getBaseSelectValue(val, index) {
            var detailVm = this;
            var displayValue = detailVm.col.selDetail.displayValue;
            var hiddenValue = detailVm.col.selDetail.hiddenValue;
            var result = detailVm.col.selDetail.sqlDataSource.filter(function (item) { return item[hiddenValue] == val; });
            if (result.length > 0) {
                return result[0][displayValue];
            }
            else {
                return "";
            }
        },
        //计算合计行数据
        calSummaryData() {
            var detailVm = this;
            if (detailVm.IsShowSummary) {
                // var listSummaryColumns = [];
                var summaryData = [];
                for (var i = 0; i < detailVm.columns.length; i++) {
                    if (detailVm.columns[i].colTotalFunction && !detailVm.columns[i].isHidden) {
                        // listSummaryColumns.push(detailVm.columns[i].colTotalFunction + "(" + detailVm.columns[i].colName + ") as " + detailVm.columns[i].colName + "");
                        summaryData.push({
                            desc: detailVm.columns[i].colDesc,
                            name: detailVm.columns[i].colName,
                            value: 0
                        });
                    }
                }
                detailVm.summaryData = summaryData;
                detailVm.$parent.setBottomTotalParam(detailVm.summaryData)
                // $.post("http://106.15.33.39:9981/SQLData/GetSummaryData", {
                //     programId: localStorage.getItem("programId"),
                //     tableName: detailVm.params.physicalTableName,
                //     filterCondition: detailVm.params.filterCondition,
                //     listSummaryColumns: listSummaryColumns
                // }, function (data) {
                //     if (data.status == 200) {
                //         if (data.data.ds.length > 0) {
                //             for (var key in data.data.ds[0]) {
                //                 detailVm.$set(detailVm.summaryData, key, data.data.ds[0][key]);
                //             }
                //             alert(JSON.stringify(detailVm.summaryData))
                //         }
                //     }
                // }, "json");
            }
        },
        //合计行
        // getSummaries(param) {
        //     var detailVm = this;
        //     const { columns, data } = param;
        //     const sums = [];
        //     columns.forEach((column, index) => {
        //         if (index === 0) {
        //             sums[index] = '合计';
        //             return;
        //         }
        //         else {
        //             sums[index] = '';
        //         }
        //     });
        //     var colIndex = 0;
        //     for (var i = 0; i < detailVm.columns.length; i++) {
        //         if (!detailVm.columns[i].isHidden) {
        //             if (detailVm.summaryData[detailVm.columns[i].colName]) {
        //                 sums[colIndex + 2] = detailVm.summaryData[detailVm.columns[i].colName].toString();
        //             }
        //             colIndex++;
        //         }

        //     }
        //     return sums;
        // },

        // getBottomTotal() {
        //     var detailVm = this;
        //     for (var i in detailVm.summaryData) {
        //         var totalNum = 0;
        //         for (var j in detailVm.tableData) {
        //             totalNum += parseFloat(detailVm.tableData[j][detailVm.summaryData[i].name])
        //         }
        //         detailVm.summaryData[i].value = totalNum;
        //     }
        //     return detailVm.summaryData;
        // },

        /*系统函数开始*/
        //ajax查询
        post(sql, param, callBack, async) {
            var detailVm = this;
            if (async == undefined) {
                async = false;
            }
            var dsResult = detailVm.postDataSource.filter(function (item) { return item.sql == sql && JSON.stringify(item.param) == JSON.stringify(param) && item.async == async });
            if (dsResult.length > 0) {
                var data = dsResult[0].data;

                if (data.status == 200) {
                    callBack(data.data);
                }
                else {
                    layer.open({
                        content: data.msg,
                        skin: 'msg',
                        time: 3
                    });
                }
            }
            else {
                param["sql"] = sql;
                param["programId"] = localStorage.getItem("programId");
                $.ajax({
                    url: "http://106.15.33.39:9981/SQLData/QuerySql",
                    type: "post",
                    dataType: "json",
                    async: async,
                    data: param,
                    success: function (data) {
                        detailVm.postDataSource.push({
                            sql: sql,
                            param: param,
                            async: async,
                            data: data
                        });

                        if (data.status == 200) {
                            callBack(data.data);
                        }
                        else {
                            layer.open({
                                content: data.msg,
                                skin: 'msg',
                                time: 3
                            });
                        }
                    }
                });
            }
        },
        //设置固定参数值
        setFixedValues(name, value) {
            var detailVm = this;
            detailVm.$set(detailVm.fixedValues, name, value);
        },
        //设置固定参数值，并且更改现有表值数据
        setFixedValuesByTrans(name, value) {
            var detailVm = this;
            detailVm.$set(detailVm.fixedValues, name, value);
            var isUpdate = false;
            for (var i = 0; i < detailVm.tableData.length; i++) {
                var oldValue = detailVm.tableData[i][name];
                //如果值不相同的时候，则重新赋值
                if (oldValue != value) {
                    isUpdate = true;
                    detailVm.tableData[i][name] = value;
                    //列change事件
                    var currentCol = detailVm.columns.filter(function (item) { return item.colName == name });
                    if (currentCol.length > 0) {
                        if (currentCol[0].colChange) {
                            detailVm.handlerControlChange(i, currentCol[0].colChange);
                        }
                        if (currentCol[0].colCallBack) {
                            detailVm.handlerControlChange(i, currentCol[0].colCallBack);
                            if (i == 0) {
                                detailVm.refreshCol();
                                detailVm.handlerControlChange(i, currentCol[0].colCallBack);
                            }
                        }
                    }
                    if (detailVm.crud.create.filter(function (item) { return item.objNo == detailVm.tableData[i].objNo; }).length == 0) {
                        if (detailVm.crud.update.filter(function (item) { return item.objNo == detailVm.tableData[i].objNo; }).length == 0) {
                            detailVm.crud.update.push(detailVm.tableData[i]);
                        }
                        else {
                            detailVm.crud.update = detailVm.crud.update.filter(function (item) { return item.objNo != detailVm.tableData[i].objNo; });
                            detailVm.crud.update.push(detailVm.tableData[i]);
                        }
                    }
                    else {
                        detailVm.crud.create = detailVm.crud.create.filter(function (item) { return item.objNo != detailVm.tableData[i].objNo; });
                        detailVm.crud.create.push(detailVm.tableData[i]);
                    }
                }
            }
            if (isUpdate) {
                //刷新列
                detailVm.refreshCol();
            }
        },

        //获取列下拉数据源对象
        queryColumnObj(rowDataSource, colName, condition, objValue) {
            var detailVm = this;
            var newColumns = JSON.parse(JSON.stringify(rowDataSource));
            var returnValue = "";
            var currentCol = newColumns.filter(function (item) { return item.colName == colName });
            if (currentCol.length > 0) {
                currentCol = currentCol[0];
                try {
                    for (var i = currentCol.selDetail.sqlDataSource.length - 1; i >= 0; i--) {
                        var isCondition = false;
                        eval("isCondition = currentCol.selDetail.sqlDataSource[i]." + condition);
                        if (isCondition) {
                            returnValue = currentCol.selDetail.sqlDataSource[i][objValue];
                            break;
                        }
                    }
                } catch (e) {
                    returnValue = "";
                }

                return returnValue;
            }
        },
        execChangeEvent(formCol, eventType) {
            var detailVm = this;
            var currentCol = detailVm.columns.filter(function (item) { return item.colName == formCol });
            if (currentCol.length > 0) {
                if (eventType == 1) {
                    detailVm.handlerControlChange(detailVm.tableData[detailVm.pickerIndex[0]], currentCol[0].colCallBack);
                }
                else if (eventType == 2) {
                    detailVm.handlerControlChange(detailVm.tableData[detailVm.pickerIndex[0]], currentCol[0].colChange);
                }
            }
        },
        //系统默认常量时间
        getSYDefaultValue(funcName) {
            var detailVm = this;
            if (funcName.indexOf("getdate()") >= 0) {
                var date;
                if (funcName == "getdate()") {
                    date = new Date();
                } else {
                    date = new Date();
                    Date.prototype.DateAdd = function (strInterval, Number) {
                        var dtTmp = this;
                        switch (strInterval) {
                            case 's': return new Date(Date.parse(dtTmp) + (1000 * Number));
                            case 'n': return new Date(Date.parse(dtTmp) + (60000 * Number));
                            case 'h': return new Date(Date.parse(dtTmp) + (3600000 * Number));
                            case 'd': return new Date(Date.parse(dtTmp) + (86400000 * Number));
                            case 'w': return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
                            case 'q': return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number * 3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
                            case 'm': return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
                            case 'y': return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
                        }
                    }
                    var addFun = funcName.split('.')[1];
                    eval("date=date." + addFun);
                }
                return FLD.getFormatDate(date);
            }
            else {
                return funcName;
            }
        },
        /*系统函数结束*/
        /*change获取数据机制处理
          用于change过程中反复调用后台处理的机制问题
        */
        changeDataAjax: function (datasource, query) {
            var detailVm = this;
            var dsResult = detailVm.changeDataSource.filter(function (item) { return item.datasource == datasource && item.query == query; });
            if (dsResult.length == 0) {
                var returnData = [];
                var sql = "select * from (" + datasource + ")t";
                sql += " where " + query;
                $.ajax({
                    url: "http://106.15.33.39:9981/SQLData/GetDataBySql",
                    async: false,
                    data: {
                        programId: localStorage.getItem("programId"),
                        sql: sql
                    },
                    type: "post",
                    dataType: 'json',
                    success: function (data) {
                        if (data.status == 200) {
                            returnData = data.data.ds;
                            detailVm.changeDataSource.push({
                                datasource: datasource,
                                query: query,
                                data: returnData
                            });
                        }
                    }
                });
                return returnData;
            }
            else {
                return dsResult[0].data;
            }
        }
    }

})